package com.wanmait.server.controller;

import com.google.common.collect.Maps;
import com.wanmait.common.response.BaseResponse;
import com.wanmait.common.response.StatusCode;
import com.wanmait.common.util.PageUtile;
import com.wanmait.common.util.ValidatorUtil;
import com.wanmait.model.entity.Staff;
import com.wanmait.server.service.StaffService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @ClassName StaffController
 * @Author 羽
 * @Date 2020/12/15 15:25
 * @Version 1.0
 **/
@Controller
@RequestMapping("/staff")
public class StaffController extends UserController{

    @Autowired
    private StaffService staffService;
    /**
    * TODO
    * @author 羽
    * @date 2020/12/27 20:52
    * 员工信息
    **/
    @RequestMapping("list")
    @ResponseBody
    @RequiresPermissions(value = {"staff:list"})
    public BaseResponse list(@RequestParam Map<String,Object> paramMap){
        BaseResponse response = new BaseResponse(StatusCode.Success);
        Map<String,Object> map = Maps.newHashMap();
        try{
            log.info("员工信息分页列表模糊查询：{}",paramMap);
            PageUtile page = staffService.pageFindAll(paramMap);
            map.put("page",page);
        }catch (Exception e){
            response = new BaseResponse(StatusCode.Fail);
        }
        response.setData(map);
        return response;
    }

    /**
    * TODO
    * @author 羽
    * @date 2020/12/27 23:39
    * 新增
    **/
    @RequestMapping("save")
    @RequiresPermissions(value = {"staff:save"})
    @ResponseBody
    public BaseResponse save(@RequestBody @Validated Staff staff, BindingResult result){
        System.out.println();

        String res= ValidatorUtil.checkResult(result);
        if (StringUtils.isNotBlank(res)){
            return new BaseResponse(StatusCode.InvalidParams.getCode(),res);
        }
        if (StringUtils.isBlank(staff.getPassword())){
            return new BaseResponse(StatusCode.PasswordCanNotBlank);
        }
        BaseResponse response=new BaseResponse(StatusCode.Success);

        try {
            staff.setCreateById(getLoginStaffInfo().getId());
            staff.setUpdateById(getLoginStaffInfo().getId());
            staffService.saveStaff(staff);

        }catch (Exception e){
            response=new BaseResponse(StatusCode.Fail.getCode(),e.getMessage());
        }
        return response;

    }

    /**
    * TODO
    * @author 羽
    * @date 2020/12/28 1:04
    *根据id获取此用户的详情
    **/
    @RequestMapping("info/{id}")
    @RequiresPermissions(("staff:list"))
    @ResponseBody
    public BaseResponse info(@PathVariable Integer id){
        log.info("用户模块~获取详情：{}",id);
        BaseResponse response=new BaseResponse(StatusCode.Success);
        Map<String,Object> resMap=Maps.newHashMap();
        try {

            resMap.put("user",staffService.getInfo(id));
            response.setData(resMap);
        }catch (Exception e){
            response=new BaseResponse(StatusCode.UpdatePasswordFail);
        }
        return response;
    }
    /**
    * TODO
    * @author 羽
    * @date 2020/12/28 3:17
    * 修改员工信息
    **/
    @RequestMapping("update")
    @RequiresPermissions(("staff:update"))
    @ResponseBody
    public BaseResponse update(@RequestBody @Validated Staff staff,BindingResult result){
        String res= ValidatorUtil.checkResult(result);
        if (StringUtils.isNotBlank(res)){
            return new BaseResponse(StatusCode.InvalidParams.getCode(),res);
        }
        BaseResponse response=new BaseResponse(StatusCode.Success);
        try {
            log.info("用户模块~修改用户：{}",staff.toString());

            staff.setUpdateById(getLoginStaffInfo().getId());
            staffService.updateUser(staff);
        }catch (Exception e){
            response=new BaseResponse(StatusCode.Fail.getCode(),e.getMessage());
        }
        return response;
    }




}
